package org.BooCrack.bookstore.pay.controller.admin;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.BooCrack.bookstore.common.pojo.BSResult;
import org.BooCrack.bookstore.common.utils.Response;
import org.BooCrack.bookstore.common.utils.ResponseResult;
import org.BooCrack.bookstore.model.entity.Role;
import org.BooCrack.bookstore.model.service.IRoleService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
//@RequestMapping("/admin/role")
@RequestMapping("admin/V7")
@RequiresPermissions("role-manage")
@Api(value = "v1", tags = "8-5.后台管理系统管理管理角色模块接口")
public class RoleController {

    @Autowired
    private IRoleService roleService;


    @GetMapping("/role")
    @RequiresPermissions("role-list")
    @ApiOperation(value = "角色列表", notes = "角色列表")
    public ResponseResult<Map<String, Object>> roleList(Model model){
        List<Role> allRoles = roleService.findAllRoles();
        model.addAttribute("allRoles", allRoles);
//        return "admin/role/list";

        Map<String, Object> map = new HashMap<>();
        map.put("allRoles", allRoles);
        return Response.createOkResp("findAllRoles()",map);
    }


    @GetMapping("/role/{roleId}")
    @RequiresPermissions("role-edit")
    @ApiOperation(value = "角色修改", notes = "角色修改")
    public ResponseResult<Map<String, Object>> toEdit(@PathVariable("roleId") int roleId, Model model){

        Role role = roleService.findById(roleId);

        model.addAttribute("role", role);

//        return "admin/role/edit";
        Map<String, Object> map = new HashMap<>();
        map.put("role", role);
        return Response.createOkResp("findById",map);
    }



    @DeleteMapping("/role/{roleId}")
    @RequiresPermissions("role-delete")
    @ApiOperation(value = "角色删除", notes = "角色删除")
    public ResponseResult<Map<String, Object>> deleteRole(@PathVariable("roleId")int roleId){

        roleService.deleteById(roleId);

//        return "forward:../list";

        return Response.createOkResp("deleteById");
    }




    @PostMapping("/role")
    @RequiresPermissions("role-add")
    @ApiOperation(value = "角色添加", notes = "角色添加")
    public ResponseResult<Object> addRole(Role role){
        roleService.addRole(role);
        return Response.createOkResp("addRole");
        }


        @PutMapping("/role")
        @RequiresPermissions("role-edit")
        @ApiOperation(value = "角色修改", notes = "角色修改")
        public ResponseResult<Map<String, Object>> updateRole(Role role, Model model){
            roleService.updateRole(role);
            model.addAttribute("saveMsg", "保存成功");
//        return "forward:echo/"+role.getRoleId();
            Map<String, Object> map = new HashMap<>();
            map.put("saveMsg", "保存成功");
            map.put("role",role);
            return Response.createOkResp("updateRole",map);
        }


        @PutMapping("/role/updatePrivilege")
        @ResponseBody
        @RequiresPermissions("role-edit")
        @ApiOperation(value = "角色权限修改", notes = "角色权限修改")
        public ResponseResult<Object> updateRolePrivilege(int[] ids, int roleId){
            BSResult bsResult = roleService.updateRolesPrivilege(ids,roleId);
//        return bsResult;
            return Response.createOkResp("bsResult");
        }

    }